<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUISApp
 *
 * @author jianfeng
 */
class BUIApp extends BUINode {

    const View_AppTopBar = 'AppTopBar';

    public $layoutView = null;
    public $views = null;
    protected $screen = null;
    protected $screenFlow = null;

    public function __construct($node = NULL) {
        parent::__construct($node);
        $this->sApp = $node;
        $basePath = $this->node->getConfigManager()->getConfigValue('basePath');
        $this->uiPath = $basePath . '.ui';

        $uiPath = $this->uiPath;

        //$uiPath = $node->uiPath;

        if ($this->modelPath == NULL)
            $this->modelPath = $uiPath . '.models';

        if ($this->screenPath == NULL)
            $this->screenPath = $uiPath . '.screens';

        if ($this->screenflowPath == NULL)
            $this->screenflowPath = $uiPath . '.screenflows';

        if ($this->widgetPath == NULL)
            $this->widgetPath = $uiPath . '.widgets';

        if ($this->uiControlPath == NULL)
            $this->uiControlPath = $uiPath . '.caontrols';

        yii::import($this->modelPath . '.*');
        yii::import($this->screenPath . '.*');
        yii::import($this->widgetPath . '.*');
        yii::import($this->uiControlPath . '.*');

        //$paths = $node->getConfig()->getConfig('paths.basePath');
        //$uiPath = $paths['uiPath'];

        if ($this->viewPath == NULL)
            $this->viewPath = $uiPath . ".themes." . Blyn::app()->getAppUI()->getTheme() . '.views';

        if ($this->formPath == NULL)
            $this->formPath = $uiPath . ".themes." . Blyn::app()->getAppUI()->getTheme() . '.forms';
    }

    public function getViewPath($viewName = NULL) {
        $viewPath = $this->uiPath . ".themes." . Blyn::app()->getAppUI()->getTheme() . '.views';
        $theme = Blyn::app()->getAppUI()->getTheme();

        //$filePath = $path . '/' . $viewName . '.php';
        //if not find view, then search preload app to find viewPath
        if ($viewName != NULL) {
            $viewPath.='.' . $viewName;
            $path = yii::getPathOfAlias($viewPath);
            if (!file_exists($path . '.php')) {
                $app = $this->node;
                $preloadApps = $app->preLoadAppList;
                foreach ($preloadApps as $app) {
                    //$path = yii::getPathOfAlias('application.application.app');
                    $aViewPath = 'application.application.app.' . get_class($app) . ".ui.themes." . $theme . '.views';
                    if ($viewName != NULL) {
                        $aViewPath.='.' . $viewName;
                    }
                    $aPath = yii::getPathOfAlias($aViewPath);
                    $aFile = $aPath . '.php';
                    if (file_exists($aFile))
                        return $aViewPath;
                }
                //if not find file exist in all apps, return false
                //return FALSE;
            }
            if (!is_dir($path)) {//return exist alias path from common app
                $app = $this->node;
                $preloadApps = $app->preLoadAppList;
                foreach ($preloadApps as $app) {
                    //$path = yii::getPathOfAlias('application.application.app');
                    $aViewPath = 'application.application.app.' . get_class($app) . ".ui.themes." . $theme . '.views';
                    $aViewPath .='.' . $viewName;
                    $aPath = yii::getPathOfAlias($aViewPath);
                    if (is_dir($aPath))
                        return $aViewPath;
                }
            }
        }

        return $viewPath;
    }

    public function onRender($data = NULL) {
        parent::onRender($data);
        if ($this->views == NULL)
            $this->views = array();

        //add views
        $this->views['registerAppScript'] = 'registerAppScript';
        $this->views['appTopBar'] = 'app_topBar';

        $configViews = $this->getNode()->getConfigManager()->getConfigList('appMeta.ui.views', TRUE);

        if ($configViews != NULL) {
            foreach ($configViews as $name => $view) {
                $this->views[$name] = $view;
            }
        }

        //add controls
        if (Blyn::app()->getActiveOperationFlow() != NULL) {
            $uiOpFlow = Blyn::app()->getActiveOperationFlow()->getUIManager();
        } else {
            $uiOpFlow = new BUIOperationFlow();
        }
        $this->addControl($uiOpFlow);
    }

    public function getLayoutView($data = NULL) {

        //parent::getLayoutView($data);
        $this->layoutView = 'app_layout';

        $configLayoutView = $this->getNode()->getConfigManager()->getConfigValue('appMeta.ui.layoutView');

        if ($configLayoutView != NULL) {
            $this->layoutView = $configLayoutView;
        }

        return $this->layoutView;
    }

    public function renderOperation($operation, $data = null) {

        //$this->setActiveOperation($operation);      
        //$app = $this->sApp;
        //call function in app to execute some jobs before rendering
        //$operation->run($data);
        //$app->doOperation($operation->getName(), $data);
        //$app->beforeRenderOperation($operation, $data);
        //get operation again because doOperation may change data in activeOperation
        //$operation = Blyn::app()->getActiveOperation();
        //$app->afterRenderOperation($operation, $data);
    }

    public function getUrl($data = NULL) {

        $ajaxUrl = $this->getAjaxUrl($data);

        if ($ajaxUrl != NULL)
            return $ajaxUrl;

        return $this->buildUrl(Blyn::app()->getActiveSpace()->getId(), $this->sApp->getId(), NULL, $data);
    }

    public function getStartScreenFlow($data = NULL) {
        $uiConfig = $this->getNode()->getConfigManager()->getConfigList('appMeta.ui.startScreenFlow');
        $configFlow = isset($uiConfig['screenFlow']) ? $uiConfig['screenFlow'] : NULL;

        if ($configFlow != NULL) {

            $class = 'BUIScreenFlow';
            if (isset($configFlow['class']))
                $class = $configFlow['class'];

            $screenFlow = new $class();

            if (isset($configFlow['layoutView']))
                $this->screen->setLayoutView($configFlow['layoutView']);

            if (isset($configFlow['views'])) {
                $views = $configFlow['views'];
                $screenFlow->addViews($views);
            }

            return $screenFlow;
        }

        return parent::getStartScreenFlow($data);
    }

    public function getStartScreen($data = NULL) {
        $uiConfig = $this->getNode()->getConfigManager()->getConfigList('appMeta.ui.startScreen');
        $configScreen = isset($uiConfig['screen']) ? $uiConfig['screen'] : NULL;

        if ($configScreen != NULL) {

            $class = 'BUIScreen';
            if (isset($configScreen['class'])) {
                $className = $configScreen['class'];
                if (class_exists($className))
                    $class = $className;
            }

            $screen = new $class();

            if (isset($configScreen['layoutView']))
                $this->screen->setLayoutView($configScreen['layoutView']);

            if (isset($configScreen['views'])) {
                $views = $configScreen['views'];
                $screen->addViews($views);
            }

            return $screen;
        }

        return parent::getStartScreen($data);
    }

    public function getAjaxLink($name, $data = array(), $sender = NULL, $route = 'site/index', $updateControl = NULL) {
        if ($sender == NULL)
            $sender = $this;

        $currentApp = $this->getNode();
        $activeApp = Blyn::app()->getActiveApp();

        $currentSpace = $currentApp->getCurrentSpace();
        $activeSpace = Blyn::app()->getActiveSpace();

        if ($currentSpace->getId() != $activeSpace->getId()) {
            return Blyn::app()->getUIManager()->getAjaxLink($name, $data, $sender);
        } else if ($currentApp->getId() != $activeApp->getId()) {
            return Blyn::app()->getActiveSpace()->getUIManager()->getAjaxLink($name, $data, $sender);
        } else {
            return parent::getAjaxLink($name, $data, $sender, $route, $updateControl);
        }
    }

    public function getAjaxContainerId($data = NULL) {
        return 'b_show_app_content';
    }

    public function getAngularControllerDefine($data = NULL) {
        return 'bngAppController';
    }

}

?>
